package work2_27;

import java.util.Random;
import java.util.Scanner;

public class Question8 {
    /**
     * 请查看Random、StringBuilder相关API，
     * 定义方法，获取一个包含4个字符的验证码，
     * 每一位字符是随机选择的字母和数字，
     * 可包含a-z,A-Z,0-9。例如7a5t
     *
     * @return
     */
    public static String getYanZhengMa(){
        StringBuilder sb=new StringBuilder();
        String s="";
        for (char c = 'A'; c <'Z' ; c++) {
            sb.append(c);
        }
        for (char c = 'a';  c<'z' ; c++) {
            sb.append(c);
        }
        for (int i = 0; i < 10; i++) {
            sb.append(i);
        }
        Random random=new Random();
        for (int i = 0; i < 4; i++) {
            int index= random.nextInt(sb.length());
            s+=sb.charAt(index);
        }
        return s;
    }
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(true){
            String code=getYanZhengMa();
            System.out.println("生成的验证码为："+code);
            System.out.println("亲输入验证码：");
            String s=sc.nextLine();
            if(code.equalsIgnoreCase(s)){
                System.out.println("输入正确");
                break;
            }else{
                System.out.println("输入错误，请重新输入……");
            }
        }
    }


}

